package com.netease.nie.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.netease.nie.base.controllers.BaseController;
import com.netease.nie.base.utils.CustomWebUtils;
import com.netease.nie.constants.LogTypeConstants;
import com.netease.nie.dto.ClickDto;
import com.netease.nie.dto.CreditInUserInfoDto;
import com.netease.nie.dto.PageViewDto;
import com.netease.nie.dto.UserLoginDto;
import com.netease.nie.service.CreditService;
import com.netease.nie.utils.LogUtils;
import com.netease.nie.utils.UserLoginUtils;

/**
 * 日志记录controller
 * 
 * @author Derek Xu
 * @CreateDate Aug 12, 2016
 */
@RestController
@RequestMapping("/api/log")
public class LogController extends BaseController {
	
	@Autowired
	private CreditService creditService;
	
	private static final String NTES_SESS = "NTES_SESS";
	
	private static final String NTES_PASSPORT = "NTES_PASSPORT";
	
	private static final String NTES_YD_SESS = "NTES_YD_SESS";
	
	/**
	 * 通用日志接口
	 */
	@RequestMapping(value = "/common.jpg", method = RequestMethod.GET)
	public void logCommon(HttpServletRequest request, 
			@RequestParam(value="log_type", required=false) Integer logType,
			@RequestParam(value="url", required=false) String url,
			@RequestParam(value="url_code", required=false) Integer urlCode,
			@RequestParam(value="click_type", required=false) Integer clickType,
			@RequestParam(value="click_id", required=false) Integer clickId) {
		if (logType == null || url == null) {
			return;
		}
		
		Long userId = (Long) request.getAttribute("userId");
		String userIdStr = userId == null ? "" : String.valueOf(userId);
		
		String ip = CustomWebUtils.getIp(request);
		String cookieId = CustomWebUtils.getCookieValue(request, NTES_SESS);
		if (cookieId == null) {
			cookieId = CustomWebUtils.getCookieValue(request, NTES_PASSPORT);
		}
		if (cookieId == null) {
			cookieId = CustomWebUtils.getCookieValue(request, NTES_YD_SESS);
		}
		String browser = CustomWebUtils.getUserAgent(request);
		
		UserLoginDto userLoginDto = UserLoginUtils.getUserLoginDto(request);
		String accountId = "";
		if (userLoginDto != null) {
			accountId = userLoginDto.getSsn();
		}
		
		Integer accountVip = 0, leftPoint = 0, expirePoint = 0;
		if (userId != null) {
			CreditInUserInfoDto creditInUserInfoDto = creditService.queryCreditInUserInfoDtoByUserId(userId);
			accountVip = creditInUserInfoDto.getLevel();	//用户等级
			leftPoint = creditInUserInfoDto.getCurrentCredit();	//可用积分
			expirePoint = creditInUserInfoDto.getReadyToExpireCredit();	//即将过期积分
		}
		
		/**
		 * 页面访问日志
		 */
		if (logType == LogTypeConstants.PAGE_VIEW.getCode()) {	
			PageViewDto pageViewDto = new PageViewDto();
			pageViewDto.setAccountId(accountId);
			pageViewDto.setUserId(userIdStr);
			pageViewDto.setAccountVip(accountVip);
			pageViewDto.setLeftPoint(leftPoint);
			pageViewDto.setExpirePoint(expirePoint);
			pageViewDto.setIp(ip);
			pageViewDto.setCookieId(cookieId);
			pageViewDto.setUrl(url);
			pageViewDto.setUrlCode(urlCode);
			pageViewDto.setBrowser(browser);
			
			LogUtils.logPageView(pageViewDto);
		} 
		
		/**
		 * 点击监控日志
		 */
		else if (logType == LogTypeConstants.CLICK.getCode()) {	
			ClickDto clickDto = new ClickDto();
			clickDto.setAccountId(accountId);
			clickDto.setUserId(userIdStr);
			clickDto.setAccountVip(accountVip);
			clickDto.setLeftPoint(leftPoint);
			clickDto.setExpirePoint(expirePoint);
			clickDto.setIp(ip);
			clickDto.setCookieId(cookieId);
			clickDto.setUrl(url);
			clickDto.setClickType(clickType);
			clickDto.setClickId(clickId);
			clickDto.setBrowser(browser);
			
			LogUtils.logClick(clickDto);
		}
	}
	
}
